package com.wwwarehouse.financialcenter.fragment.moneywarehouse.bills;

import android.os.Bundle;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.financialcenter.R;
import com.wwwarehouse.financialcenter.bean.bill.BillOrderGoodsResponseBean;
import com.wwwarehouse.financialcenter.bean.bill.BillRecordResponseBean;
import com.wwwarehouse.financialcenter.bean.bill.SerializableMap;
import com.wwwarehouse.financialcenter.constant.FinancialConstant;
import com.wwwarehouse.financialcenter.fragment.base.MyBaseViewPagerFragment;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.wwwarehouse.common.constant.Constant.sp_User_Id;

/**
 * @author Joseph on 2017/11/15.
 *         <p/>
 *         订购商品主页
 */

public class BillOrderGoodsPagerFragment extends MyBaseViewPagerFragment {
    private String mBillUkid;
    private String mPayType;
    private List<String> mRelationContentIds;

    private Map mMap;

    private boolean isFirst = true;

    private static final int REQUEST_GET_ORDER_DETAILS = 0;

    @Override
    public void setTitle() {
        if (peekFragment()instanceof BillOrderGoodsPagerFragment ){
            mActivity.setTitle(getString(R.string.financial_center_order_goods_pager));
        }
    }

    @Override
    protected void loadDatas() {
        Bundle bundle = getArguments();

        if (bundle != null) {
            BillRecordResponseBean.BillRecordDetailsBean billRecordDetailsBean = bundle.getParcelable("details");

            mBillUkid = billRecordDetailsBean.getBillUkid();
            mPayType = billRecordDetailsBean.getPayType();
            mRelationContentIds = billRecordDetailsBean.getRelationContentIds();
        }
    }

    @Override
    public void onSuccess(CommonClass commonClass, int what) {
        switch (what) {
            case REQUEST_GET_ORDER_DETAILS:
                if ("0".equals(commonClass.getCode())) {
                    BillOrderGoodsResponseBean billOrderGoodsResponseBean = JSON.parseObject(commonClass.getData().toString(), BillOrderGoodsResponseBean.class);
                    int total = billOrderGoodsResponseBean.getTotal();
                    if (total <= 0){
                        if (isFirst) {
                            isFirst = false;
                            mLoadingView.setVisibility(View.VISIBLE);
                            mLoadingView.showEmptyView(true);
                            mLoadingView.setEmptyListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    mLoadingView.showProgressView(true);
                                    isFirst = true;
                                    requestDatas();
                                }
                            });
                        } else {
                            ShowEmptyView();
                        }
                        return;
                    }
                    isFirst = false;
                    Bundle bundle = new Bundle();
                    SerializableMap serializableMap = new SerializableMap();
                    serializableMap.setMap(mMap);
                    bundle.putSerializable("map", serializableMap);

                    SetFragmentAndPage(total, 7, new BillOrderGoodsChildFragment().getClass().getName(), bundle);
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void requestDatas() {
        mMap = new HashMap();

        mMap.put("billUkid", mBillUkid);
        mMap.put("payType", mPayType);
        mMap.put("relationContentIds", mRelationContentIds);
        mMap.put("page", 1);
        mMap.put("size", 1);
        mMap.put("userId", sp.getValue(sp_User_Id));

        httpPost(FinancialConstant.URL_LIST_ORDER_DETAILS, mMap, REQUEST_GET_ORDER_DETAILS, false, null);
    }
}
